package cn.kgc.ssm.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.web.entity.Member;
import cn.kgc.ssm.web.mapper.MemberMapper;
import cn.kgc.ssm.web.response.ResponseResult;
import cn.kgc.ssm.web.service.MemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/7/31
 * @description
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public ResponseResult memberPage(Integer page, Integer limit, String memberName, String level) {

        PageHelper.startPage(page,limit);
        List<Member> members =  memberMapper.selectList(memberName,level);

        PageInfo<Member> memberPageInfo = new PageInfo<>(members);
        long total = memberPageInfo.getTotal();

        return ResponseResult.success().data(members).count(total);
    }

    @Override
    public ResponseResult addMember(Member member) {

        //  判定会员名是否存在
        String memberName = member.getMemberName();

        Member member1 =   memberMapper.selectOne(memberName);

        if (ObjectUtil.isNotEmpty(member1)){
            return ResponseResult.fail().message("会员名已存在");
        }

        // 设置会员默认密码
        String defaultPassword = SecureUtil.md5("111111");
        member.setPassword(defaultPassword);

        memberMapper.addMember(member);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult editMember(Member member) {

        memberMapper.editMember(member);

        return ResponseResult.success();
    }

    @Transactional
    @Override
    public ResponseResult deleteMember(Integer[] ids) {

        memberMapper.batchDelete(ids);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult resetPassword(Integer id) {

        String defaultPassword = SecureUtil.md5("111111");

        memberMapper.resetPwd(id,defaultPassword);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult registerMember(Member member) {
        //  判定会员名是否存在
        String memberName = member.getMemberName();
        Member member1 =   memberMapper.selectOne(memberName);

        if (ObjectUtil.isNotEmpty(member1)){
            return ResponseResult.fail().message("会员名已存在");
        }

        // 2. 加密密码
        String s = SecureUtil.md5(member.getPassword());
        member.setPassword(s);


        memberMapper.registerMember(member);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult login(String memberName, String password, HttpSession session) {

        Member member =   memberMapper.selectMember(memberName,SecureUtil.md5(password));
        // 存储会员信息到当前session
        session.setAttribute("member",member);

        if (ObjectUtil.isEmpty(member)){
            return ResponseResult.fail().message("会员名或密码错误");
        }
        return ResponseResult.success();
    }
}
